<?php 
$req_path="../";
include($req_path."includes/ini.php");
include($req_path."includes/db.php");

class M_User extends DB{
    private $rowCount=0;
    public $table_name="tb_user";
	private $result_val=array();
    function M_User(){
		parent::__construct();
    }
    
    function loadData($name='',$user='', $gid=0,$did=0){
        
        $sqlscript= "SELECT u.id, u.username,u.password ,u.name, DATE_FORMAT(u.lastaccess,'%d/%m/%y') as lastaccess , g.name as groupname,d.name as departmentname, u.`status` ";
		$sqlscript.= " FROM tb_user u ";
		$sqlscript.= " LEFT JOIN tb_group g on g.id = u.gid ";
		$sqlscript.= " LEFT JOIN tb_department d on d.id = u.did ";
		$sqlscript.= " WHERE 1=1 ";
		if ($name !=''){
			$sqlscript.= " and u.name like '".$name."'" ;
		}
		if ($user !=''){
			$sqlscript.= " and u.username like '".$user."'" ;
		}
		if ($gid!=0){
			$sqlscript.= " and u.gid = ".$gid ;
		}
		if ($did!=0){
			$sqlscript.= " and u.did = ".$did ;
		}
        
        $result = $this->ExecuteData($sqlscript);
        $this->rowCount = $result->RecordCount();
        return $result;
    }
	function Login($user,$passwd){
		$sql = " SELECT id,username, name, gid , date_format(lastaccess,'%d/%m/%Y %H:%i') as login_date
				FROM tb_user WHERE status = 'Y' ";
		$sql.=" AND username='".$user."' and password='".$passwd."'";
		$result = $this->ExecuteData($sql);
		//print_r($sql);
		$rows = $result->RecordCount();
		if ($rows > 0 ){
			$this->LastLogin($user,$passwd);
			$this->result_val=$result->FetchRow(); 
			
			return 1;
		}else {
			return 0;
		}
	}
	function getUserLoginDetail(){
		return $this->result_val;
	}
	function LastLogin($user, $password){
		$sql = " UPDATE tb_user SET lastaccess = now() WHERE username='".$user."' and password='".$password."'";
		
		$this->ExecuteData($sql);
	}
	function CheckUserAuthen($uid=0){
		$sql = "SELECT s.url as name_eng,g.v, g.a, g.e, g.d, g.col1 as ap
		From tb_user u
		left join tb_group_permission g on u.gid= g.gid
	    LEFT join tb_screen_permission s on s.id=g.sid
		where u.id='".$uid."'";
	
		$result = $this->ExecuteData($sql);
		$arr=array();
		while($row = $result->FetchRow()){
			$arr[$row["name_eng"]] = $row;
		}
		
		return $arr;//$result->FetchRow();
	}
	
    function getRowCount(){
        
        return $this->rowCount;
        
    }	
	function getUserGroup($type=''){
	 
		$sqlscript= "select id, name from tb_group where 1=1 ";
		if($type!=''){
			$sqlscript .= " and status = '".$type."' ";
		}
		$sqlscript .=" Order by id asc ";
		
		$result = $this->ExecuteData($sqlscript);
		while($row = $result->FetchRow()){
			$ar[$row["name"]] = $row["id"];
		}
		return $ar;	
	}
	function getDepartment($status=''){
	$sqlscript= "select id, name from tb_department where 1=1 ";
		if($status!=''){
			$sqlscript .= " and status = '".$status."' ";
		}
		$sqlscript .=" Order by id asc ";
		
		$result = $this->ExecuteData($sqlscript);
		while($row = $result->FetchRow()){
			$ar[$row["name"]] = $row["id"];
		}
		return $ar;	
	}
	
    function insertData($val){
            if (isset($val)){
				$sqlInsert=$this->InsertSqlScript("SELECT username,password,name,gid, did, status,createdate,createby FROM ". $this->table_name." where 1=2 ", $val);
				$this->ExecuteData($sqlInsert);
            }
    }
    function updateData($id,$val){
            if(isset($val)){
				$sqlUpdate=$this->UpdateSqlScript("SELECT  username,password,name,gid, did, status,updatedate,updateby  FROM ". $this->table_name." where id='".$id."'" ,$val);
				 $this->ExecuteData($sqlUpdate);
				// return $sqlUpdate;
            }
    }
	function updateProfile_name($id, $name){
		$sql = " UPDATE tb_user SET name='".$name."' WHERE id=".$id;
		$this->ExecuteData($sql);
	}
	function updateProfile($id, $name ,$password){
		$sql = " UPDATE tb_user SET name='".$name."' , password='".$password."' WHERE id=".$id;
		$this->ExecuteData($sql);
	}
    function deleData($id){
            if(isset($id)){
			$sqlDelete = "Delete FROM ". $this->table_name." where id='".$id."'";
			$this->ExecuteData($sqlDelete);
			return 1;
            }
    }
    
}
?>
